from docx import Document
from docx.shared import Pt
from win32com.client import Dispatch,DispatchEx
import win32com
import win32com.client
import os
import re
import shutil
import win32com.client as win32com
import pythoncom
import docx


root_dir = os.getcwd()
path = os.path.join(root_dir, "test")
dest_path = os.path.join(root_dir, "new")
new_text = "This product contains software whose rights holders license it on the terms of the GNU General Public License, version 2 (GPLv2) and/or other open source software licenses. We will provide you and any third party with the source code of the software licensed under an open source software license from: https://gitee.com/src-openeuler/, you can obtain corresponding source code by searching package name and tag"

def process_header_footer(doc):
    for section in doc.sections:
        # 页眉
        header = section.header
        if header is not None:
            print(1)
            header_picture = header.add_table(1,3,2)
            header_picture.text = "tttttttttt"
            for paragraph in header.paragraphs:
                print(paragraph.style)
                # paragraph.text = "ttttttttttttt\taaaa\t1111"
                # paragraph.style = document.styles["Header"]
                # for table in paragraph.tables:
                #     print(table)
                #     print(2)
                    # if run.element.xml.startswith("<w:drawing"):
                    #     run.add
        # 页脚
        # footer = section.footer
        # if footer is not None:
        #     for paragraph in footer.paragraphs:
        #         print(paragraph)
        #         for run in paragraph.runs:
        #             if "HUAWEI Confidential" in run.text:
        #                 new_text_footer = run.text.replace("HUAWEI Confidential", "")
        #                 run.text = new_text_footer

def main():
    try:
        file_list = os.listdir(path)
        # doc_list = list(filter(lambda s ".doc" in s or ".docx" in s, file_list))
        file_names = [f for f in os.listdir('test') if f.endswith('.docx')]
        for file_name in file_names:
            demo_file = dest_path + '\\' + "demo.docx"
            demo_doc = docx.Document(demo_file)

            print(demo_file)

            file_path = path + '\\' + file_name
            doc = docx.Document(file_path)

            print(file_path)

            is_replace = False
            for i,paragraph in enumerate(doc.paragraphs):
                if paragraph.text.startswith("This product contains software"):
                    is_replace = True
                    demo_doc.add_paragraph(new_text)
                    continue
                elif paragraph.text.startswith("This offer is valid to anyone") and is_replace:
                    is_replace = False
                elif is_replace:
                    continue
                demo_paragrph = demo_doc.add_paragraph()
                demo_paragrph.paragraph_format.line_spacing = paragraph.paragraph_format.line_spacing
                demo_paragrph.paragraph_format.alignment = paragraph.paragraph_format.alignment
                # demo_paragrph.
                for run in paragraph.runs:
                    new_run = demo_paragrph.add_run(run.text)
                    new_run.bold = run.bold
                    new_run.italic = run.italic
                    new_run.underline = run.underline
                    new_run.style = run.style
                    new_run.font.name = run.font.name
                    new_run.font.size = run.font.size
            demo_doc.save(dest_path + '\\' + file_name)
        #     app = win32com.client.Dispatch('Word.Application')
        #     doc = app.Document.Open(file_path)
        #     doc.Content.Copy()
        #     doc.close()
        #     word = win32com.client.DispatchEx('Word.Application')
        #     doc1 = word.Documents.Open(path+ r'\sample.docx')
        #     s = word.Selection
        #     s.MoveRight(1, doc1.Content.End)
        #     s.Paste()
        #     doc1.close()
            
    except Exception as err:
        print(err)
if __name__ == '__main__':
    main()
